'use strict';

module.exports = app => {
    const {router, controller, middleware} = app;
    const jwt = middleware.jwt(app.config.jwt);

    // 添加全局中间件
    router.use(jwt);
    router.get('/admin/dict_arr', controller.admin.dictionary.getAllDictList);
    router.post('/admin/upload', controller.common.upload.uploadToQiNiu);
    // 用户管理分组
    router.group({name: 'user', prefix: '/admin'}, router => {
        // 登录相关路由(不需要jwt验证)
        router.post('/login', controller.admin.user.login);
        router.post('/logout', controller.admin.user.logout);
        router.get('/info', controller.admin.user.getUserInfo);
        router.put('/user/:id/status', controller.admin.user.updateStatus);
        // 用户管理路由
        router.resources('', '/user', controller.admin.user);
    });

    // 字典管理分组
    router.group({name: 'dictionaries', prefix: '/admin'}, router => {
        router.post('/dictionary-details/import', controller.admin.dictionaryDetail.import);
        router.get('/dictionary-details/:key', controller.admin.dictionaryDetail.getDictDetailList);
        router.put('/dictionary-details/batch-sort', controller.admin.dictionaryDetail.updateSort);
        router.resources('', '/dictionaries', controller.admin.dictionary);
        router.resources('', '/dictionary-details', controller.admin.dictionaryDetail);
    });

    // 文章管理分组
    router.group({name: 'articles', prefix: '/admin'}, router => {
        router.resources('', '/articles', controller.admin.article);
        router.put('/articles/:id/status', controller.admin.article.updateStatus);
    });

    // 帮助中心管理分组
    router.group({name: 'help_questions', prefix: '/admin'}, router => {
        router.resources('', '/help_questions', controller.admin.helpQuestions);
        router.post('/help_questions/import', controller.admin.helpQuestions.import);
        router.put('/help_questions/:id/status', controller.admin.helpQuestions.updateStatus);
    });

    // 岗位管理分组
    router.group({name: 'positions', prefix: '/admin'}, router => {
        router.resources('', '/positions', controller.admin.position);
        router.post('/positions/import', controller.admin.position.import);
        router.put('/positions/batch-sort', controller.admin.position.updateSort);
        router.put('/positions/:id/status', controller.admin.position.updateStatus);
    });


    // 广告管理
    router.resources('ads', '/admin/ads', controller.admin.ads);
    router.put('/admin/ads/:id/status', controller.admin.ads.updateStatus);

    // 公告
    router.group({name: 'notices', prefix: '/admin'}, router => {
        router.resources('', '/notices', controller.admin.notice);
        router.put('/notices/:id/status', controller.admin.notice.updateStatus);
    });
    // 发布的岗位分组
    router.group({name: 'job-openings', prefix: '/admin'}, router => {
        router.resources('', '/job-openings', controller.admin.jobOpening);
        router.put('/job-openings/:id/status', controller.admin.jobOpening.updateStatus);
    });
};
